package tests.update;

import static org.junit.Assert.fail;

import java.util.ArrayList;

import model.Budget;
import model.BudgetFactory;
import model.Line;
import model.Subline;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import cc.test.bridge.SublineInterface;

/*
 * 
 * Attempts to change the name a 1 specific subline of a budget
 * that contains 2 sublines
 * 
 */

public class SublineUpdateNameTest00
{
	Budget budget;
	BudgetFactory factory;
	Line line;
	Subline subline;

	@Before
	public void setUp() throws Exception
	{
		System.out.println("Begining test: " +this.getClass());
		System.out.println("Setup");
		
		factory = new BudgetFactory();
		budget = (Budget) factory.create();
        budget.setName("Test budget");
        budget.setDescription("This is a test budget.");
        
        line = (Line) budget.createLine();
		budget.add(line);
		
		subline = (Subline) line.createSubline();
		subline.setName("Subline");
		line.add(subline);
		budget.commit();
	}

	@After
	public void tearDown() throws Exception
	{
		System.out.println("Tear down");
		
		Helper.delete(subline, "Sublines", WorkletContext.getInstance());
		Helper.delete(line, "Lines", WorkletContext.getInstance());
		Helper.delete(budget, "Budgets", WorkletContext.getInstance());
	}

	@Test
	public void test()
	{
		try
		{
			
			ArrayList<SublineInterface> sublines =  line.fetchSublines();
			Subline subline = (Subline) sublines.get(0);
			subline.setName("NewSubline");

   		    Boolean successful = budget.commit();

	        if(!successful)
	        {
	           	fail("Subline failed to update");
	        }
		}
		catch(Exception e)
		{
			fail("Fetch all sublines failed with exception " + e);
		}
	}
}